from typing import List

from fastapi import APIRouter, Depends
import json
from sqlalchemy import select, desc, func, distinct
from sqlalchemy.ext.asyncio import AsyncSession
from apis.deps import get_db
from schemas import MailCreate, MailUpdate, MailOut as Mail, Result, ResultPlus
from crud import mail
from utils import resp_200, IdNotExist
from models import Mail as MailModel

router = APIRouter()


@router.post("/createMail", summary="新增收件人邮箱")
async def create_mail(mail_in: MailCreate, db: AsyncSession = Depends(get_db)):
    await mail.create(db=db, obj_in=mail_in)
    return resp_200(msg=f"成功添加收件人邮箱.")


@router.get("/getMail", summary='查询收件人邮箱')
async def get_project(pageIndex: int = 1, pageSize: int = 10, queryName: str = '', db: AsyncSession = Depends(get_db)):
    get_mail_arr = await mail.get_multi(db=db, pageIndex=pageIndex, pageSize=pageSize, queryName=queryName)
    print(get_mail_arr)
    return resp_200(msg=f"查询成功", data=get_mail_arr)


@router.put("/updateMail", summary="更新收件人邮箱")
async def update_mail(id: int, mail_in: MailUpdate, db: AsyncSession = Depends(get_db)):
    await mail.update(db=db, id=id, obj_in=mail_in)
    return resp_200(msg=f"修改了id为{id}的收件人邮箱.")


@router.delete("/deleteMail", summary="删除收件人邮箱")
async def delete_mail(id: int, db: AsyncSession = Depends(get_db)):
    await mail.remove(db=db, id=id)
    return resp_200(msg=f"删除了id为{id}的收件人邮箱.")
